﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BuildPlannerLib.Buildings;
using BuildPlannerLib.PlayerActions;
using BuildPlannerLib.SimEvents;
using BuildPlannerLib.Terran.Buildings;
using BuildPlannerLib.Terran.Enums;

namespace BuildPlannerLib.Terran.PlayerActions
{
    // Derived class from Action for attaching an existing add-on to a Barracks/Factory/Starport
    public class AttachAddon : PlayerAction
    {
        // Field for the building that will produce the addon
        public IndividualAddonBuilding IndividualBuilding;

        // Properties for the type of addon and type of building
        private AddonType addonType;
        private string buildingType;
        public AddonType AddonType
        {
            get { return addonType; }
            set { addonType = value; }
        }
        public string BuildingType
        {
            get { return buildingType; }
            set { buildingType = value; }
        }

        // Constructor with parameters for build, building type, and addon type
        public AttachAddon(Build build, string buildingType, AddonType addonType)
        {
            Build = build;
            BuildingType = buildingType;
            AddonType = addonType;
            // Landing takes 2 seconds.  We'll assume that player is floating the building directly overhead to receive the addon.
            TimeDuration = 200;
            Description = "Attach " + AddonType + " to " + BuildingType;
            LongDescription = "Attaching unused " + AddonType + " onto existing " + BuildingType;
        }

        // Evaluates whether the player is Terran, whether there is a building of the specified type with no addon, and whether an idle addon exists
        public override bool RequirementsFilled()
        {
            Error = null;

            TerranPlayer terranPlayer = Build.Player as TerranPlayer;
            if (terranPlayer == null)
            {
                Error = "This building can only be built by Terran players.";
                return false;
            }

            switch (AddonType)
            {
                case AddonType.Reactor:
                    if (terranPlayer.IdleReactor == 0)
                    {
                        Error = "No addons available.";
                        return false;
                    }
                    break;
                case AddonType.TechLab:
                    if (terranPlayer.IdleTechLab == 0)
                    {
                        Error = "No addons available.";
                        return false;
                    }
                    break;
                default:
                    Error = "Invalid addon type.";
                    return false;
            }

            AddonBuilding building = Build.Player.GetBuilding(BuildingType) as AddonBuilding;

            if (building == null)
            {
                Error = "Invalid building.";
                return false;
            }

            bool buildingAvailable = building.IndividualBuildings.Exists(delegate(IndividualBuilding i)
            {
                IndividualAddonBuilding a = i as IndividualAddonBuilding;
                return a.AddonType == AddonType.None;
            });

            if (!buildingAvailable)
            {
                Error = "There are no " + BuildingType + " available to attach to an addon.";
                return false;
            }

            return base.RequirementsFilled();
        }

        // Evaluates if there is there is a building of the specified type with the no addon idle
        public override bool StartCheck()
        {
            AddonBuilding building = Build.Player.GetBuilding(BuildingType) as AddonBuilding;

            bool buildingAvailable = building.IndividualBuildings.Exists(delegate(IndividualBuilding i)
            {
                IndividualAddonBuilding a = i as IndividualAddonBuilding;
                return (a.AddonType == AddonType.None && !i.InUse);
            });

            if (!buildingAvailable)
                return false;

            return base.StartCheck();
        }

        // Adds an idle addon, removes the addon from the existing building, puts the building in-use, and adds the event for finishing the action
        public override void StartAction()
        {
            AddonBuilding building = Build.Player.GetBuilding(BuildingType) as AddonBuilding;
            TerranPlayer terranPlayer = Build.Player as TerranPlayer;

            base.StartAction();

            IndividualBuilding = building.IndividualBuildings.Find(delegate(IndividualBuilding i)
            {
                IndividualAddonBuilding a = i as IndividualAddonBuilding;
                return (!i.InUse && a.AddonType == AddonType.None);
            }) as IndividualAddonBuilding;

            switch (AddonType)
            {
                case AddonType.Reactor:
                    terranPlayer.IdleReactor--;
                    IndividualBuilding.AddReactor();
                    break;
                case Enums.AddonType.TechLab:
                    terranPlayer.IdleTechLab--;
                    IndividualBuilding.AddTechLab();
                    break;
            }
        }

        // Sets the building to not in-use
        public override void FinishAction()
        {
            base.FinishAction();

            IndividualBuilding.FinishProduction();
            IndividualBuilding.FinishAddonProduction();
        }
    }
}
